import Taro from "@tarojs/taro";
import {getFundings, getFunding} from '../services/api';

export default {
  namespace: 'funding',

  state: {
    list: [],
    item: {},
  },

  effects: {
    // * create({payload}, {call, put}) {
    //   const result = yield call(createFunding, payload);
    //   if (result && result.code === 0) {
    //     Taro.showToast({
    //       title: result.message,
    //       icon: 'none',
    //       duration: 1000,
    //     });
    //
    //     setTimeout(() => {
    //       Taro.navigateBack();
    //     }, 1000);
    //   }
    // },

    * getAll({payload}, {call, put}) {
      const result = yield call(getFundings, payload);
      if (result && result.code === 0) {
        yield put({
          type: 'save',
          payload: {
            list: result.data,
          },
        });
      }
    },

    * get({payload}, {call, put}) {
      const result = yield call(getFunding, payload);
      if (result && result.code === 0) {
        yield put({
          type: 'save',
          payload: {
            item: result.data,
          },
        });
      }
    },
  },

  reducers: {
    save(state, {payload}) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
